package com.zzy.study.socketANDnio;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;

public class TestFileChannel {

	public static void main(String[] args) {
		FileInputStream fis = null;
		FileOutputStream fos = null;
		try {
			fis = new FileInputStream("C:\\Users\\eason\\Desktop\\CFS.docx");
			fos = new FileOutputStream("C:\\Users\\eason\\Desktop\\CFSs.docx");
			FileChannel fic = fis.getChannel();
			FileChannel foc = fos.getChannel();
			ByteBuffer buffer = ByteBuffer.allocate(1024);
			while(fic.read(buffer)!=-1){
				buffer.flip();
				while(buffer.hasRemaining()){ //----------------------(1)
					foc.write(buffer);
				}
				buffer.clear();
			}

		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			if (null != fis) {
				try {
					fis.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
			if (null != fos) {
				try {
					fos.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}

	}

}
